﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SC2parser.Model;

namespace SC2parser.UserControls
{
    public partial class LineGraph : UserControl
    {
        Player player1;
        Player player2;
        Font font;

        /// <summary>
        /// Instatiates a new LineGraph
        /// </summary>
        public LineGraph()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Instantiates a new LineGraph with data from the two
        /// </summary>
        /// <param name="player1">The first player to draw data from</param>
        /// <param name="player2">The second player to draw data from</param>
        public LineGraph(Player player1, Player player2)
        {
            InitializeComponent();
            this.player1 = player1;
            this.player2 = player2;
        }

        /// <summary>
        /// Instantiates a new linegraph with the data from a match
        /// </summary>
        /// <param name="match">The match to draw data from</param>
        public LineGraph(Match match)
        {
            InitializeComponent();
            this.player1 = match.PlayerOne;
            this.player2 = match.PlayerTwo;
        }

        /// <summary>
        /// Overrides the OnPaint event from the usercontrol
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics gfx = e.Graphics;
            //DrawLineGraph(300, 700, gfx, 400);
            if (DesignMode == true && player2 == null || player1 == null)
                DrawFullLineGraph(300, 700, gfx);
            else
            {
                DrawFullLineGraph(player1.Voters.Count, player2.Voters.Count, gfx);
            }
        }

        public void DrawLineGraph(float votesPlayerOne, float votesPLayerTwo, Graphics gfx, int lineGraphSize)
        {
            float totalVotes = votesPlayerOne + votesPLayerTwo;
            float percentVotesPlayerOne = votesPlayerOne / totalVotes * 100, percentVotesPlayerTwo = votesPLayerTwo / totalVotes * 100;
            float fillFloatPlayerOne = ((lineGraphSize / 2) / 100) * percentVotesPlayerOne, fillFloatPlayerTwo = ((lineGraphSize / 2) / 100) * percentVotesPlayerTwo;

            Rectangle rec = new Rectangle(20, 20, lineGraphSize, 20);
            gfx.DrawRectangle(new Pen(Color.Black), rec);
            Brush fillBrushPlayerOne, fillBrushPlayerTwo;
            fillBrushPlayerOne = new SolidBrush(Color.Red);
            fillBrushPlayerTwo = new SolidBrush(Color.Blue);
            gfx.FillRectangle(fillBrushPlayerOne, rec.X + lineGraphSize / 2 - fillFloatPlayerOne, rec.Y, fillFloatPlayerOne, 20);
            gfx.FillRectangle(fillBrushPlayerTwo, rec.X + lineGraphSize / 2, rec.Y, fillFloatPlayerTwo, 20);
            gfx.DrawLine(new Pen(Color.Black, 5), new Point(rec.Width / 2 + 20, rec.Height / 2 + 40), new Point(rec.Width / 2 + 20, rec.Height / 2));

            Brush textBrush;
            textBrush = new SolidBrush(Color.Black);
            gfx.DrawString(percentVotesPlayerOne.ToString() + "%", new Font(Font, FontStyle.Bold), textBrush, lineGraphSize / 4, 50);
            gfx.DrawString(percentVotesPlayerTwo.ToString() + "%", new Font(Font, FontStyle.Bold), textBrush, (lineGraphSize / 4) * 3, 50);
        }

        /// <summary>
        /// Draws a linegraph with two floats as data
        /// </summary>
        /// <param name="votesPlayerOne">The number of votes for the first player</param>
        /// <param name="votesPLayerTwo">The number of votes for the second player</param>
        /// <param name="gfx"></param>
        public void DrawFullLineGraph(float votesPlayerOne, float votesPLayerTwo, Graphics gfx)
        {
            float lineGraphSize = this.Width;
            float totalVotes = votesPlayerOne + votesPLayerTwo;
            float percentVotesPlayerOne = votesPlayerOne / totalVotes * 100, percentVotesPlayerTwo = votesPLayerTwo / totalVotes * 100;
            float fillFloatPlayerOne = ((lineGraphSize ) / 100) * percentVotesPlayerOne;
            float fillFloatPlayerTwo = ((lineGraphSize) / 100) * percentVotesPlayerTwo;

            Rectangle rec = new Rectangle(this.Padding.Left, this.Padding.Top, this.Width - this.Padding.Horizontal, this.Height - this.Padding.Vertical);
            gfx.DrawRectangle(new Pen(Color.White), rec);

            Brush fillBrushPlayerOne, fillBrushPlayerTwo;
            fillBrushPlayerOne = new SolidBrush(Color.Red);
            fillBrushPlayerTwo = new SolidBrush(Color.Blue);

            gfx.FillRectangle(fillBrushPlayerOne, rec.X, rec.Y, fillFloatPlayerOne, this.Height);
            gfx.FillRectangle(fillBrushPlayerTwo, rec.X + fillFloatPlayerOne, rec.Y, fillFloatPlayerTwo, this.Height);

            //gfx.DrawLine(new Pen(Color.Black, 5), new Point(rec.X + (int)fillFloatPlayerOne, rec.Y), new Point(rec.X + (int)fillFloatPlayerOne, rec.Y + 20));

            Brush textBrush;
            font = new Font(Font, FontStyle.Bold);
            textBrush = new SolidBrush(Color.White);
            float halfHeight = rec.Bottom - (this.Height + gfx.MeasureString("arbitraryText", font).Height)/2;

            string playerTwoText = percentVotesPlayerTwo.ToString() + "%";
            string playerOneText = percentVotesPlayerOne.ToString() + "%";

            gfx.DrawString(playerOneText, new Font(Font, FontStyle.Bold), textBrush, fillFloatPlayerOne / 2 - gfx.MeasureString(playerOneText, font).Width / 2, halfHeight);
            gfx.DrawString(playerTwoText, font, textBrush, fillFloatPlayerOne + (fillFloatPlayerTwo / 2) - gfx.MeasureString(playerTwoText, font).Width/2 , halfHeight);
        }

    }
}
